package com.quickcode.example.collections;

import com.google.common.collect.Maps;
import com.quickcode.annotation.UnSafe;
import lombok.Data;

import java.util.Collections;
import java.util.Map;

/**
 * Created by 张波 on 2019/10/23.
 */
@UnSafe
public class ImmutableExample3 {

    private static Map<String,DemoClass> map = Maps.newHashMap();

    static {
        DemoClass a = new DemoClass();
        a.setId("a");
        a.setName("zhangsan");
        map.put("a",a);
        DemoClass b = new DemoClass();
        a.setId("b");
        a.setName("lisi");
        map.put("b",b);
        //执行过unmodifiable后不允许再进行修改了，下述main方法进行赋值会报异常
        map = Collections.unmodifiableMap(map);
    }

    public static void main(String[] args) {
        DemoClass a = map.get("a");
        a.setName("wangwu");
        System.out.println(map.get("a").getName());
    }

    @Data
    static class DemoClass {
        String id;

        String name;

    }
}
